package com.future.bantang.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.bantang.R;


/**
 * Created by zzh on 2016/1/6.
 * 自定义titlebar
 */
public class TitleBarView extends RelativeLayout {

    private Context context;
    private ImageView mIco;
    private TextView mTitle;

    public TitleBarView(Context context) {
        super(context);
        inintView(context, null, 0);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inintView(context, attrs, 0);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inintView(context, attrs, defStyleAttr);
    }

    private void inintView(Context context, AttributeSet attrs, int defStyleAttr) {
        this.context=context;
        //加载布局
        View view = inflate(context, R.layout.titlebar_view_lay, this);
        //获取子控件
        mIco = (ImageView) view.findViewById(R.id.img_myTitleBar_ico);
        mTitle = (TextView) view.findViewById(R.id.txt_myTitleBar_title);
        //解析attrs
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
        //获取属性
        String titleText=typedArray.getString(R.styleable.TitleBarView_setTitle);
        int icoImg = typedArray.getResourceId(R.styleable.TitleBarView_setIco, 0);
        int bgColor = typedArray.getColor(R.styleable.TitleBarView_setBackground, getResources().getColor(R.color.app_bg_title_bar));
        int titleColor = typedArray.getColor(R.styleable.TitleBarView_setTitleColor, getResources().getColor(R.color.app_text_white));
        //将属性设置到子控件上
        //将属性值设置到控件上
        mTitle.setText(titleText);
        mTitle.setTextColor(titleColor);
        if (icoImg!=0) {
            mIco.setImageResource(icoImg);
        }
        this.setBackgroundColor(bgColor);

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    /**
     * 提供给外界控制ico
     * @return
     */
    public ImageView getmIco(){
        return mIco;
    }
}
